package com.qding.park.adapter.bsg.adapter.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.qding.park.common.utils.DateUtils;
import com.qding.park.common.utils.ListUtil;
import com.qding.park.manager.pojo.ap.ApPark;
import com.qding.park.manager.pojo.ap.ApParkTypeInfo;
import com.qding.park.manager.pojo.ap.ApSyncPageInfo;
import com.qding.park.manager.pojo.ap.ApUserCarInfo;

/**
 * @ClassName: ResponseParser
 * @Description: 应答报文解析
 * @author Comsys-Administrator
 * @date 2016年9月18日 下午4:39:33
 *
 */
public class ResponseParser {

	private static final Log _log = LogFactory.getLog(ResponseParser.class);

	public static List<ApParkTypeInfo> parking_parkType_parser(String text) {
		try {
			_log.info(text);
			Document doc = DocumentHelper.parseText(text);
			Element root = doc.getRootElement();

			List<Element> parkInInfos = root.selectNodes("parkTypeInfo");
			String clientID = root.attributeValue("clientID");

			List<ApParkTypeInfo> approachRecords = new ArrayList<ApParkTypeInfo>();
			for (int i = 0; i < parkInInfos.size(); i++) {
				Element element = parkInInfos.get(i);
				// String clientID =
				// element.selectSingleNode("clientID").getText();
				String parkTypeID = element.selectSingleNode("parkTypeID").getText();
				String parkTypeName = element.selectSingleNode("parkTypeName").getText();

				String carTypeCode = element.selectSingleNode("carTypeCode").getText();
				String busyID = element.selectSingleNode("busyID").getText();
				String busyName = element.selectSingleNode("busyName").getText();
				String duringStart = element.selectSingleNode("duringStart").getText();
				String duringEnd = element.selectSingleNode("duringEnd").getText();
				String inParkTime = element.selectSingleNode("inParkTime").getText();
				String inParkTimeCost = element.selectSingleNode("inParkTimeCost").getText();
				String perTime = element.selectSingleNode("perTime").getText();
				String perCost = element.selectSingleNode("perCost").getText();
				String duringHighestFee = element.selectSingleNode("duringHighestFee").getText();
				String highestFeeOneDay = element.selectSingleNode("highestFeeOneDay").getText();

				ApParkTypeInfo ap = new ApParkTypeInfo();
				ap.setBusyID(busyID);
				ap.setBusyName(busyName);
				ap.setDuringEnd(duringEnd);
				ap.setDuringEnd(duringEnd);
				ap.setDuringHighestFee(Integer.valueOf(duringHighestFee));
				ap.setDuringStart(duringStart);
				ap.setHighestFeeOneDay(Integer.valueOf(highestFeeOneDay));
				ap.setInParkTime(inParkTime);
				ap.setInParkTimeCost(Integer.valueOf(inParkTimeCost));
				ap.setParkId(clientID);
				ap.setParkTypeID(Integer.valueOf(parkTypeID));
				ap.setParkTypeName(parkTypeName);
				ap.setPerTime(StringUtils.isBlank(carTypeCode)?0:Integer.valueOf(perTime));
				ap.setPerCost(Integer.valueOf(perCost));
				ap.setCarTypeCode(StringUtils.isBlank(carTypeCode)?-1:Integer.valueOf(carTypeCode));

				approachRecords.add(ap);

			}
			return approachRecords;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

	public static ApSyncPageInfo parking_SyncMember(String text) {
		ApSyncPageInfo ap = new ApSyncPageInfo();
		_log.info(text);
		try {
			Document doc = DocumentHelper.parseText(text);
			Element root = doc.getRootElement();

			String sync_count = root.attributeValue("sync_Count");
			String sync_ID = root.attributeValue("sync_ID");
			ap.setSync_Count(Integer.valueOf(sync_count));
			ap.setSync_ID(sync_ID);
			
			if(Integer.valueOf(sync_count)==0){
				return ap;
			}
			List<Element> apList = root.selectNodes("parkMemberInfo");

			List<ApUserCarInfo> userifolist = new ArrayList<ApUserCarInfo>();

			if (!ListUtil.isEmpty(apList)) {
				for (Element ele : apList) {
					String memberID = ele.selectSingleNode("memberID").getText();
					String cardCode = ele.selectSingleNode("cardCode").getText();
					String carNumber = ele.selectSingleNode("carNumber").getText();
					if("粤B66770".equals(carNumber)){
						System.out.println();
					}
					String cardSN = ele.selectSingleNode("cardSN").getText();
					String member_Name = ele.selectSingleNode("member_Name").getText();
					String contact_Tel1 = ele.selectSingleNode("contact_Tel1").getText();
					String expireDate = ele.selectSingleNode("expireDate").getText();
					String cardState = ele.selectSingleNode("cardState").getText();
					String cardType = ele.selectSingleNode("cardType").getText();
					String carType = ele.selectSingleNode("carType").getText();
					String monthFee = ele.selectSingleNode("monthFee").getText();
					String payCard = ele.selectSingleNode("payCard").getText();
					String vehicle_Brands = ele.selectSingleNode("vehicle_Brands").getText();
					String car_Color = ele.selectSingleNode("car_Color").getText();
					String home_Address = ele.selectSingleNode("home_Address").getText();
					String work_Address = ele.selectSingleNode("work_Address").getText();
					String cardName=ele.selectSingleNode("cardName")==null?"":ele.selectSingleNode("cardName").getText();
					//后期新加的属性做版本性　　要做版本性兼容
					String carpos=ele.selectSingleNode("carpos")==null?"":ele.selectSingleNode("carpos").getText();
					String requireguy=ele.selectSingleNode("requireguy")==null?"":ele.selectSingleNode("requireguy").getText();
					String requiretime=ele.selectSingleNode("requiretime")==null?"":ele.selectSingleNode("requiretime").getText();
					
					ApUserCarInfo apUserCarInfo = new ApUserCarInfo();
					apUserCarInfo.setMemberID(memberID);
					apUserCarInfo.setCardCode(cardCode);
					apUserCarInfo.setCarNumber(carNumber);
					apUserCarInfo.setCardSN(cardSN);
					apUserCarInfo.setMember_Name(member_Name);
					apUserCarInfo.setContact_Tel1(contact_Tel1);
					apUserCarInfo.setExpireDate((StringUtils.isBlank(expireDate) || "20000101".equals(expireDate))
							? null : DateUtils.parseDate(expireDate, DateUtils.DATE_SHORT_DATEONLY));
					apUserCarInfo.setCardState(Integer.valueOf(cardState));
					apUserCarInfo.setCardType(Integer.valueOf(cardType));
					apUserCarInfo.setCarType(Integer.valueOf(carType));
					apUserCarInfo.setMonthFee(Integer.valueOf(monthFee));
					apUserCarInfo.setPayCard(Integer.valueOf(payCard));
					apUserCarInfo.setVehicle_Brands(vehicle_Brands);
					apUserCarInfo.setCar_Color(car_Color);
					apUserCarInfo.setHome_Address(home_Address);
					apUserCarInfo.setWork_Address(work_Address);
					apUserCarInfo.setCardDescript(cardName);
					apUserCarInfo.setCarpos(carpos);
					apUserCarInfo.setRequireguy(requireguy);
					apUserCarInfo.setRequiretime(StringUtils.isBlank(requiretime) ? null : DateUtils.parseDate(requiretime, DateUtils.DATE_LONG_DATE));
					userifolist.add(apUserCarInfo);
				}
			}
			ap.setPageData(userifolist);
			return ap;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static List<ApPark> parking_ParkingClientInfo(String text) {
		// ApSyncPageInfo ap = new ApSyncPageInfo();
		// List<ApParkTypeInfo> resultlist=new ArrayList<ApParkTypeInfo>();
		_log.info(text);
		try {
			Document doc = DocumentHelper.parseText(text);
			Element root = doc.getRootElement();

			List<Element> apList = root.selectNodes("parkingClientInfo");

			List<ApPark> resultlist = new ArrayList<ApPark>();

			if (!ListUtil.isEmpty(apList)) {
				for (Element ele : apList) {
					String clientID = ele.selectSingleNode("clientID").getText();
					String appVer = ele.selectSingleNode("appVer").getText();
					String connState = ele.selectSingleNode("connState").getText();
					String lastRequestTime=ele.selectSingleNode("lastRequestTime").getText();

					ApPark apPark = new ApPark();
					apPark.setParkId(clientID);
					apPark.setAppVer(appVer);
					apPark.setConnState(connState);
					apPark.setLastRequestTime(StringUtils.isBlank(lastRequestTime)?null:DateUtils.parseDate(lastRequestTime, DateUtils.DATE_LONG_DATE));
					
					resultlist.add(apPark);
				}
			}
			return resultlist;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
